[CF254C]Anagram

2019-11-15
Codeforces

题意

替换最少数量的字符,使得A中的每个大写字符个数都与B中相同

输出最少替换次数以及字典序最小的方案

题解

完完全全一道傻逼题,考场脑子坏掉了

最少次数显然,输出方案就从后往前,替换字符更小或者不能再协调就替换,不然先不替换

调试记录

犯了一堆神奇的错误,还是思路实现不清晰啊

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <cstdio>
#include <cstring>
#include <algorithm>
const int maxn = 1e5 + 5;
using namespace std;
char a[maxn], b[maxn]; int n, cnt[26], c[2][26];
int main(){
// freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
scanf("%s%s", a + 1, b + 1);
int n = strlen(a + 1);
for (int i = 1; i <= n; i++) cnt[a[i] - 'A']++, cnt[b[i] - 'A']--, c[0][a[i] - 'A']++;
int res = 0, x = -1;
for (int j = 0; j < 26; j++){
res += max(cnt[j], 0);
if (cnt[j] < 0 && x == -1) x = j;
if (cnt[j] > 0) c[1][j] = cnt[j];
}
printf("%d\n", res);
for (int i = 1; i <= n; i++){
if (cnt[a[i] - 'A'] == 0) continue;
if (cnt[a[i] - 'A'] > 0){
if (a[i] - 'A' > x || c[0][a[i] - 'A'] == c[1][a[i] - 'A']){
--cnt[a[i] - 'A']; --c[0][a[i] - 'A']; --c[1][a[i] - 'A'];
a[i] = x + 'A';
++cnt[x];
while (cnt[x] >= 0 && x < 25) ++x;
} else c[0][a[i] - 'A']--;
}
} printf("%s\n", a + 1);
return 0;
}